'''
Created on Nov 29, 2010

@author: dexen
'''

import os
import time
import random

from dexen import SlaveTask

import common
from my_ind import MyInd

#houdini geometry
def develop(ind, basefilepath, filename):
    assert isinstance(ind, MyInd)
    common.enableHouModule()
    import hou
    basefile = os.path.join(basefilepath, filename)
    hou.hipFile.load(basefile)
    genotype = hou.node("/obj/geo1/genotype")
    for cnt in range(len(ind.genotype)):
        genotype.setParms({"genotype"+str(cnt+1):ind.genotype[cnt]})
        
    phenotype_geom = os.path.join(basefilepath, "phenotype")
    if not os.path.isdir(phenotype_geom):
        os.mkdir(phenotype_geom)
    file_path = os.path.join(phenotype_geom, "temp.bgeo")
    hou.node("/obj/geo1/phenotype").geometry().saveToFile(file_path)
    
    f = open(file_path, "r")
    phenotype = f.read()
    f.close()
    return phenotype
    
def dummy_develop():
    return "Genotype"

class DevTask(SlaveTask):
    def __init__(self):
        pass
    
    def __execute__(self, master_state, inds):
        print "I am in development"
        for ind in inds:
            assert isinstance(ind, MyInd)
            #ind.phenotype = dummy_develop()
            ind.phenotype = develop(ind, common.HOUDINI_BASE_FILE, "dev.hipnc")
            ind.archive_data("overhang_schema_1.bgeo", ind.phenotype)
        return inds
